Skip to main content

Json

The JSON Module for the addon API!


Functions​

isSafe​

sm.scrapcomputers.json.isSafe( root )

Returns true if a lua table is safe to convert to JSON.

A table is not safe if it (or any table inside of it) has a key that isn't a string or a number, or if it mixes key types (for example having both string and number keys).

Arguments:

  • root [ table ] The lua table to check.

Returns:

  • [ boolean ] If true, That means it's safe to convert. else this is not safe to be passed to sm.json.writeJsonString or sm.json.save!

toString​

sm.scrapcomputers.json.toString( root, safeMode, prettify, indent )

Converts a value to a JSON string.

  • If safeMode is on and root is a table that isn't safe, this errors instead of producing a string that could crash the game.
  • Tables with sequential keys (1, 2, 3, ...) become JSON arrays. Every other table (including an empty one) becomes a JSON object.
  • Number keys inside of an object are converted to strings. Any other key type will cause an error.
  • Non-finite numbers (NaN, inf and -inf) are written as null.

Arguments:

  • root [ table|string|number|boolean|nil ] The value to convert to a string.
  • safeMode [ boolean? ] If it should care about saftey or not. Defaults to true. Only turn this off if you are 100% sure the table is safe!
  • prettify [ boolean? ] If true, The JSON string would be prettier. Use this when you want to display it to the user.
  • indent [ string? ] The indentation character. Defaults to "\t"

Returns:

  • [ string ] The JSON string from a Lua table.

prettifyTable​

sm.scrapcomputers.json.prettifyTable( rootContents )

Adds syntax highlighting to the JSON tab. Content is automatically prettified.

Arguments:

  • rootContents [ table|string|number|boolean|nil ] The root contents to format.

Returns:

  • [ string ] The prettified JSON-like formatted string.

prettifyString​

sm.scrapcomputers.json.prettifyString( root )

Prettifies a JSON string by parsing it into a table and passing it into prettifyTable.

Arguments:

  • root [ string ] The raw JSON string.

Returns:

  • [ string ] The prettified result.

toJsonCompatibleTable​

sm.scrapcomputers.json.toJsonCompatibleTable( tbl )

Cleans a table by removing all keys and values that cannot be represented in JSON. This also goes through all tables inside of it.

  • Valid keys are strings and numbers.
  • Valid values are strings, numbers, booleans and tables.
  • If the table is an array, the invalid items are removed and the remaining items shift down so there are no gaps left.

Arguments:

  • tbl [ table ] The table to clean. (It will be modified.)

Returns:

  • [ table ] The cleaned table.